#!/bin/bash
## A script to handle opening urls from weechat in tmux, possibly on host maching
## Inspired by https://toxicfrog.github.io/remote-url-handling-in-weechat-and-tmux/
##
## This script is relying on the laptop opening a reverse ssh tunnel to the server
##
## Add binding for M-o #, for 1 through 9 (10 can be 0)
## /key bind meta-o1 /url_hint_replace /exec -bg weechat-open-url {url1}
## /key bind meta-o2 /url_hint_replace /exec -bg weechat-open-url {url2}
##
## You can chose between a split (split-window -h) or new (new-window) in tmux


## List of [wireguard] hosts to try for GUI viewing
## laptop, tablet
host_list="10.0.10.3 10.0.10.7"


function status_check {
    host_return="none"

    for h in ${host_list}; do
        ## Reverse SSH Tunnel Check
        ## VPN Check, nc faster than a ping
        if [ $(nc -z -w 1 ${h} 22 && echo 1 || echo 0) == 1 ]; then
            ## If host is reachable, set var and end testing
            host_return=${h}
            break
        fi
    done

    if [ ${host_return} != "none" ]; then
        host_use=${host_return}
    else
        host_use="none"
    fi
}

function source-ssh {
    ## Source SSH settings, if applicable
    local SSH_ENV="${HOME}/.ssh/environment"

    if [ -f "${SSH_ENV}" ]; then
        . "${SSH_ENV}" > /dev/null
        ps -ef 2>/dev/null | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
            pstree -up ${USER} 2>/dev/null | grep ${SSH_AGENT_PID} | grep ^ssh-agent > /dev/null || {
                __start_agent;
            }
        }
    fi
}

function open-image {
    if [[ ${host_use} != "none" ]]; then
        source-ssh
        #ssh ${host_use} env DISPLAY=:0 feh -x -F --auto-zoom "'$1'"
        ssh ${host_use} env DISPLAY=:0 pqiv "'$1'"
        #ssh ${host_use} env DISPLAY=:0 curl -s "'$1'" \| env DISPLAY=:0 swayimg -
        #ssh ${host_use} env DISPLAY=:0 curl -s "'$1'" \| env DISPLAY=:0 swayimg -
  else
        tmux new-window -c ~ bash -c "timg $1 && read -s -n 1 -p 'Press Any Key to Exit'"
    fi
}

function open-url {
    if [[ ${host_use} != "none" ]]; then
        #ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' /usr/share/qutebrowser/scripts/open_url_in_instance.sh "'$1'"
        source-ssh
        ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' xdg-open "'$1'"
  else
        tmux new-window -c ~ bash -c "w3m '$1'"
    fi
}

function open-video {
    ## Only exec if laptop connected
    if [[ ${host_use} != "none" ]]; then
        source-ssh
        ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' mpv --profile=rem "'$1'"
    fi
}

function open-sound {
    ## Only exec if laptop connected
    if [[ ${host_use} != "none" ]]; then
        source-ssh
        ssh ${host_use} env DISPLAY=:0 'XDG_RUNTIME_DIR=/run/user/$(id -u)' mpv --profile=pod "'$1'"
    fi
}

status_check

case $(echo "$1" | tr A-Z a-z) in
    *.jpg|*.jpeg|*.png|*.gif|*.svg|*:large|*.webp|*.png/raw)
        open-image "$1"
        ;;
    *youtube.com/*|*youtu.be/*|*.gifv|*.mp4|*.webm)
        open-video "$1"
        ;;
    *.mp3)
        open-sound "$1"
        ;;
    *)
        open-url "$1"
        ;;
esac
